package com.hanxing.kxcharge.module.operations.service.equipmentmodel;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.equipmentmodel.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipmentmodel.EquipmentModelDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.equipmentmodel.EquipmentModelMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 设备型号 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class EquipmentModelServiceImpl implements EquipmentModelService {

    @Resource
    private EquipmentModelMapper equipmentModelMapper;

    @Override
    public Long createEquipmentModel(EquipmentModelSaveReqVO createReqVO) {
        // 插入
        EquipmentModelDO equipmentModel = BeanUtils.toBean(createReqVO, EquipmentModelDO.class);
        equipmentModelMapper.insert(equipmentModel);
        // 返回
        return equipmentModel.getId();
    }

    @Override
    public void updateEquipmentModel(EquipmentModelSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentModelExists(updateReqVO.getId());
        // 更新
        EquipmentModelDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentModelDO.class);
        equipmentModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentModel(Long id) {
        // 校验存在
        validateEquipmentModelExists(id);
        // 删除
        equipmentModelMapper.deleteById(id);
    }

    private void validateEquipmentModelExists(Long id) {
        if (equipmentModelMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentModelDO getEquipmentModel(Long id) {
        return equipmentModelMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentModelDO> getEquipmentModelPage(EquipmentModelPageReqVO pageReqVO) {
        return equipmentModelMapper.selectPage(pageReqVO);
    }

}